package com.m3839.sdk.ym.base

import android.app.DialogFragment
import android.app.FragmentManager
import android.os.Bundle
import android.view.*
import com.m3839.sdk.ym.core.YMUIStackManager
import com.m3839.sdk.ym.ext.*

/**
 * Description:YMBaseDialogFragment
 *
 * @author 陈宝阳
 * @create 2020/8/6 10:11
 */
abstract class YMBaseDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        YMUIStackManager.instance!!.addFragment(this.tag, this)
        setStyle(
            STYLE_NO_TITLE, getStyleId("CommonDlgTheme")
        )
        parseArgs()

    }

    override fun onStart() {
        super.onStart()
        dialog.window?.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        dialog.window!!.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //布局位于状态栏下方
                //View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //全屏
                View.SYSTEM_UI_FLAG_FULLSCREEN or  //隐藏导航栏
                //View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        uiOptions = uiOptions or 0x00001000
        dialog.window!!.decorView.systemUiVisibility = uiOptions
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                //拦截返回键事件
                if(!isInterceptBackEvent(event)){
                    dismiss()
                    false
                }else
                    true
            } else
                false
        }
    }

    protected open fun isInterceptBackEvent(event: KeyEvent): Boolean {
        return false
    }


    override fun onCreateView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater!!.inflate(
            getLayoutId(layoutName()), container
        )
        initView(view)
        setContent()
        initListener()
        return view
    }

    protected abstract fun parseArgs()
    protected abstract fun layoutName(): String
    protected abstract fun initView(view: View?)
    protected abstract fun setContent()
    protected abstract fun initListener()

    override fun show(
        manager: FragmentManager,
        tag: String
    ) {
        try {
            val c = Class.forName("android.app.DialogFragment")
            val con = c.getConstructor()
            val obj = con.newInstance()
            val dismissed = c.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed[obj] = false
            val shownByMe = c.getDeclaredField("mShownByMe")
            shownByMe.isAccessible = true
            shownByMe[obj] = false

            val mTag = c.getDeclaredField("mTag")
            mTag.isAccessible = true
            mTag[obj] = tag
        } catch (e: Exception) {
            e.printStackTrace()
        }
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }

    override fun dismiss() {
        dismissAllowingStateLoss()
    }

    override fun onDestroy() {
        YMUIStackManager.instance!!.removeFragment(this.tag)
        super.onDestroy()
    }
}